package com.smarthome.core.operationservicemanager.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.service.ShCompanyExpressService;
import com.smarthome.base.service.ShResidenceExpressService;
import com.smarthome.core.operationservicemanager.CompanyExpressService;
import com.smarthome.domain.bean.ShCompanyExpress;
import com.smarthome.domain.bean.ShResidenceExpress;
import com.smarthome.domain.vo.boss.Combobox;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

/**
 * 
* @ClassName: CompanyExpressServiceImpl
* @Description: 快递公司service接口实现类
* @author cyj
* @date 2016年3月21日 下午9:04:17
*
 */
@Service("companyExpressService")
public class CompanyExpressServiceImpl implements CompanyExpressService {

	@Resource
	private ShCompanyExpressService shCompanyExpressService;
	
	@Resource
	private ShResidenceExpressService shResidenceExpressService;

	@Override
	public ShCompanyExpress getById(Long id) {
		return shCompanyExpressService.selectEntryById(id);
	}

	@Override
	public DataGrid qeuryDataGrid(ShCompanyExpress companyExpress, PageHelper page) {
		return shCompanyExpressService.quetyDataGrid(companyExpress, page);
	}

	@Override
	public Integer saveOrUpdate(ShCompanyExpress companyExpress) {
		return shCompanyExpressService.saveOrUpdate(companyExpress);
	}

	@Override
	public Integer delete(Long id) {
		return shCompanyExpressService.deleteByIds(id);
	}

	@Override
	public List<Combobox> getCombobox(Long residenceId) {
		List<Combobox> result = new ArrayList<Combobox>();
		ShResidenceExpress residenceExpress = new ShResidenceExpress();
		residenceExpress.setResidenceId(residenceId);
		
		List<Long> expressIdList = shResidenceExpressService.getExpressIdList(residenceExpress);
		
		
		if(expressIdList != null && !expressIdList.isEmpty()) {
			Long[] expressIds = expressIdList.toArray(new Long[expressIdList.size()]);
			
			List<ShCompanyExpress> expresslist = shCompanyExpressService.selectEntryListByIds(expressIds);
			
			int length = expresslist.size();
			for(int i = 0;i < length;i++) {
				if(expresslist.get(i).getStatus() != null && expresslist.get(i).getStatus().equals(1)) {
					result.add(new Combobox(expresslist.get(i).getId().toString(), expresslist.get(i).getName()));
				}
			}
		}
		return result;
	}

	@Override
	public boolean checkName(ShCompanyExpress companyExpress) {
		if(companyExpress == null || StringUtils.isBlank(companyExpress.getName()) || StringUtils.isBlank(companyExpress.getAddr())) {
			return false;
		}
		ShCompanyExpress express = new ShCompanyExpress();
		express.setName(companyExpress.getName());
		express.setAddr(companyExpress.getAddr());
		List<ShCompanyExpress> list = shCompanyExpressService.selectEntryList(express);
		if(list == null || list.isEmpty()) {
			return true;
		}else {
			if(list.get(0).getId().equals(companyExpress.getId())) {
				return true;
			}else {
				return false;
			}
		}
	}
	
	

}
